<?php


namespace app\common\model\store;

use app\common\model\BaseModel;
use app\common\model\settings\Region as RegionModel;
use think\db\BaseQuery;


/**
 * 门店订单模型
 */
class StoreLook extends BaseModel
{
    protected $pk = 'store_look_id';

    protected $name = 'store_look';

    protected $append = ['region'];

    /**
     * 关联门店logo
     */
    public function logo()
    {
        return $this->hasOne("app\\common\\model\\file\\UploadFile", 'file_id', 'logo_image_id');
    }

    /**
     * 关联供应商表
     */
    public function supplier()
    {
        return $this->belongsTo('app\\common\\model\\supplier\\Supplier', 'shop_supplier_id', 'shop_supplier_id');
    }

    /**
     * 地区名称
     */
    public function getRegionAttr($value, $data)
    {
        return [
            'province' => RegionModel::getNameById($data['province_id']),
            'city' => RegionModel::getNameById($data['city_id']),
            'region' => $data['region_id'] == 0 ? '' : RegionModel::getNameById($data['region_id']),
        ];
    }


    /**
     * 门店状态
     */
    public function getStatusAttr($value)
    {
        $status = [0 => '禁用', 1 => '启用'];
        return ['text' => $status[$value], 'value' => $value];
    }

    /**
     * 是否支持自提核销
     */
    public function getIsCheckAttr($value)
    {
        $status = [0 => '不支持', 1 => '支持'];
        return ['text' => $status[$value], 'value' => $value];
    }


    /**
     * 门店详情
     */
    public static function detail($store_id)
    {
        return (new static())->with(['logo'])->where('store_id','=',$store_id)->find();
    }

    /**
     * 坐标范围修改器
     * @param $value
     * @param $data
     * @return array|false|string
     * @Author joey
     * @date 2024/3/1 16:41
     */
    public function setScopeCoordinatesAttr($value, $data)
    {
        return isset($data['scope_coordinates']) && is_array($data['scope_coordinates']) ? json_encode($data['scope_coordinates']) : [];
    }

    /**
     *获取所有门店的配送范围
     * getScopeCoordinatesList
     * @param $shop_supplier_id
     * @param $city
     * array
     * @return array
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * static
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author kevin
     * @date 2024/5/28 17:12
     */
    public static function getScopeCoordinatesList($shop_supplier_id,$city_id=0,$store_id=0)
    {
        $map = [];
        $map[] = ['is_delete','=',0];
        if($shop_supplier_id > 0){
            $map[] = ['shop_supplier_id','=',$shop_supplier_id];
        }
        if($city_id > 0){
            $map[] = ['city_id','=',$city_id];
        }

        if($store_id > 0){
            $map[] = ['store_id','<>',$store_id];
        }

        $list = (new self)->where($map) ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->select();
        $scope_coordinates = array_column($list->toArray(),'scope_coordinates');
        $scope_coordinates_list = [];
        foreach ($scope_coordinates as $value){
            if($value){
                $scope_coordinates_list[] =  $value;
            }

        }
        return $scope_coordinates_list;
    }

    /**
     * 坐标范围获取器
     * @param $val
     * @return array|mixed
     * @Author joey
     * @date 2024/3/1 16:41
     */
    public function getScopeCoordinatesAttr($val)
    {
        return $val ? json_decode($val, true) : [];
    }

    /**
     * 获待审核数量
     * getAuditCount
     * int
     * @return int
     * @throws \think\db\exception\DbException
     * static
     * @throws \think\db\exception\DbException
     * @Author kevin
     * @date 2024/5/20 10:35
     */
    public static function getAuditCount(){
        $map = [];
        $map[] = ['check_store','=',0];
        $map[] = ['is_delete','=',0];
        return self::where($map) -> count();

    }

    /**
     * @param array $where
     * @param int $sysDel
     * @return BaseQuery
     * @author joey
     * @day 2024/03/08
     */
    public function search(array $where, int $sysDel = 0)
    {
        return $this->hasWhere('supplier', function ($query) use ($where) {
            $query->where('is_delete', 0);
        })->when(($sysDel !== null), function ($query) use ($sysDel) {
            $query->where('is_delete', $sysDel);
        })
            ->when(isset($where['store_ids']) && $where['store_ids'] !== '', function ($query) use ($where) {
                $query->whereIn('store_id', $where['store_ids']);
            })
            ->when(isset($where['store_id']) && $where['store_id'] !== '', function ($query) use ($where) {
                $query->where('store_id', $where['store_id']);
            })
            ->when(isset($where['shop_supplier_id']) && $where['shop_supplier_id'] !== '', function ($query) use ($where) {
                $query->where('shop_supplier_id', $where['shop_supplier_id']);
            })
            ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
                getModelTime($query, $where['date']);
            })
            ->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
                $query->where('status', $where['status']);
            })
            ->when(isset($where['check_store']) && $where['check_store'] !== '', function ($query) use ($where) {
                $query->where('check_store', $where['check_store']);
            })
            ->when(isset($where['is_check']) && $where['is_check'] !== '', function ($query) use ($where) {
                $query->where('is_check', $where['is_check']);
            })
            ->when(isset($where['province_id']) && $where['province_id'] !== '', function ($query) use ($where) {
                $query->where('province_id', $where['province_id']);
            })
            ->when(isset($where['city_id']) && $where['city_id'] !== '', function ($query) use ($where) {
                $query->where('city_id', $where['city_id']);
            })
            ->when(isset($where['region_id']) && $where['region_id'] !== '', function ($query) use ($where) {
                $query->where('region_id', $where['region_id']);
            })
            ->when(isset($where['is_delete']) && $where['is_delete'] !== '', function ($query) use ($where) {
                $query->where('is_delete', $where['is_delete']);
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->where(function ($query) use ($where) {
                    $query->where('linkman|phone', 'like', "%{$where['keywords']}%");
                });
            });
    }
}